package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.ErrorBook;
import com.baomidou.ant.entity.QuestionInfo;
import com.baomidou.ant.service.IErrorBookService;
import com.baomidou.ant.service.IPveInfoService;
import com.baomidou.ant.service.IQuestionInfoService;
import com.baomidou.ant.utils.MyStringUtils;
import com.baomidou.ant.vo.Request.RequestChoiceInfoVo;
import com.baomidou.ant.vo.Request.RequestPassEventVo;
import com.baomidou.ant.vo.Response.ResponseGetQuestionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/pve-info")
public class PveInfoController {
    @Autowired
    private IPveInfoService pveInfoService;
    @Autowired
    private IErrorBookService errorBookService;
    @Autowired
    private IQuestionInfoService questionInfoService;

    //关卡出题机制------根据用户信息来出题
    @GetMapping("/getQuestions/{userId}")
    public Result getQuestions(@PathVariable Integer userId){
        List<ResponseGetQuestionVo> voList=pveInfoService.getQuestion(userId);
        if(ObjectUtils.isEmpty(voList)||voList.size()<10){
            return Result.getFailure().setMsg("0");
        }
        return Result.getSuccess().setData(voList);
    }
    //正确or错误接口
    @PostMapping("/trueOrFalse")
    public Result trueOrFalse(@RequestBody RequestChoiceInfoVo requestChoiceInfoVo){
        if(pveInfoService.trueOrFalse(requestChoiceInfoVo)){
            return Result.getSuccess().setMsg("1");
        }else {
            //纪录错题本
            QueryWrapper<ErrorBook> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("user_id",requestChoiceInfoVo.getUserId());
            ErrorBook errorBook=errorBookService.getOne(queryWrapper);

            int[] errorE= MyStringUtils.toInt(errorBook.getEasyPve());
            int[] errorN= MyStringUtils.toInt(errorBook.getNormalPve());
            int[] errorH= MyStringUtils.toInt(errorBook.getHardPve());
            List<Integer> errorEasy = Arrays.stream(errorE).boxed().collect(Collectors.toList());
            List<Integer> errorNormal= Arrays.stream(errorN).boxed().collect(Collectors.toList());
            List<Integer> errorHard= Arrays.stream(errorH).boxed().collect(Collectors.toList());
            //判断题的难度,并判断是否纪录过该数据
            QuestionInfo questionInfo=questionInfoService.getById(requestChoiceInfoVo.getQuestionId());
            if (Objects.equals(questionInfo.getQuestionLevel(), "简单")) {
                if(!errorEasy.contains(questionInfo.getId())){
                    errorBook.setEasyPve(MyStringUtils.toString(requestChoiceInfoVo.getQuestionId(),errorBook.getEasyPve()));
                }
            }else if(Objects.equals(questionInfo.getQuestionLevel(), "中等")){
                if(!errorNormal.contains(questionInfo.getId())){
                    errorBook.setNormalPve(MyStringUtils.toString(requestChoiceInfoVo.getQuestionId(),errorBook.getNormalPve()));
                }
            }else{
                if(!errorHard.contains(questionInfo.getId())){
                    errorBook.setHardPve(MyStringUtils.toString(requestChoiceInfoVo.getQuestionId(),errorBook.getHardPve()));
                }
            }
            errorBookService.saveOrUpdate(errorBook);
            return Result.getFailure().setMsg("0").setData(questionInfo.getRightItem());
        }
    }

    //判断过关接口
    @PostMapping("/pass")
    public Result pass(@RequestBody RequestPassEventVo requestPassEventVo){
        for(int i:requestPassEventVo.getQuestionId()){
            if(i==0)
                return Result.getFailure().setMsg("请完成所有题目再提交!");
        }
        pveInfoService.passEvent(requestPassEventVo);
        return Result.getSuccess().setMsg("恭喜你通关了!");
    }

}
